package com.wsoft.cms.controller;

import com.github.pagehelper.PageInfo;
import com.wsoft.cms.query.announcement.AnnouncementListQuery;
import com.wsoft.cms.query.category.CategoryTreeQuery;
import com.wsoft.cms.query.comment.CommentInsertQuery;
import com.wsoft.cms.query.comment.CommentListQuery;
import com.wsoft.cms.query.comment.CommentMainListQuery;
import com.wsoft.cms.query.sign.SignListQuery;
import com.wsoft.cms.query.sign.SignQuery;
import com.wsoft.cms.service.ICmsAnnouncementCommentService;
import com.wsoft.cms.service.ICmsAnnouncementService;
import com.wsoft.cms.service.ICmsAnnouncementSignService;
import com.wsoft.cms.service.ICmsCategoryService;
import com.wsoft.cms.vo.anno.AnnouncementListVO;
import com.wsoft.cms.vo.category.CmsCategoryVO;
import com.wsoft.cms.vo.comment.CommentVO;
import com.wsoft.cms.vo.comment.MainCommentVO;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/12/27 09:17
 */
@RestController
@Tag(name = "用户端获取")
@RequestMapping("/cms/user")
public class CmsUserController {

    @Resource
    private ICmsAnnouncementService announcementService;
    @Resource
    private ICmsAnnouncementCommentService commentService;
    @Resource
    private ICmsAnnouncementSignService signService;
    @Resource
    private ICmsCategoryService categoryService;

    @Operation(summary = "新闻公告详情-用户端", description = "新闻公告详情")
    @GetMapping("/getInfo")
    public ResponseResult getInfo(@RequestParam("id") Long id) {
        return ResponseResult.success(announcementService.getInfoByUser(id));
    }

    @Operation(summary = "公告分页-用户端", description = "公告分页-用户端")
    @PostMapping("/listInfo")
    public ResponseResult listInfo(@RequestBody AnnouncementListQuery query) {
        PageInfo<AnnouncementListVO> page = announcementService.listAnno(query,true);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "一键已读-用户端", description = "一键已读-用户端")
    @PostMapping("/allRead")
    public ResponseResult allRead() {
        boolean b = announcementService.allRead();
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }


    @Operation(summary = "新增评论", description = "新增评论")
    @PostMapping("/insertComment")
    public ResponseResult insert(@RequestBody @Valid CommentInsertQuery query) {
        boolean b = commentService.insertComment(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "删除评论", description = "删除评论")
    @GetMapping("/deleteComment")
    public ResponseResult delete(@RequestParam("id") Long id) {
        return ResponseResult.success(commentService.deleteComment(id,true));
    }


    @Operation(summary = "参与报名", description = "参与报名")
    @PostMapping("/sign")
    public ResponseResult sign(@RequestBody @Valid SignQuery query) {
        boolean b = signService.sign(query,true);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "取消报名", description = "取消报名")
    @PostMapping("/cancelSign")
    public ResponseResult cancelSign(@RequestBody @Valid SignQuery query) {
        boolean b = signService.sign(query,false);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }


    @Operation(summary = "获取主评论的子评论列表", description = "获取主评论的子评论列表")
    @PostMapping("/getChildPage")
    public ResponseResult getChildPage(@RequestBody CommentListQuery query) {
        PageInfo<CommentVO> page = commentService.getChildCommentPage(query);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "获取主评论", description = "获取主评论")
    @PostMapping("/getMainPage")
    public ResponseResult getMainPage(@RequestBody CommentMainListQuery query) {
        PageInfo<MainCommentVO> page = commentService.getMainCommentPage(query);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "分类tree", description = "分类tree")
    @PostMapping("/tree")
    public ResponseResult tree(@RequestBody CategoryTreeQuery query) {
        List<CmsCategoryVO> list = categoryService.tree(query.isUser(),query.getCateId());
        return ResponseResult.success(list);
    }

    @Operation(summary = "报名记录", description = "报名记录")
    @PostMapping("/signList")
    public ResponseResult signList(@RequestBody SignListQuery query) {
        return ResponseResult.success(ResponsePage.restPage(signService.listSign(query)));
    }

}
